import {Request, Response} from "express";
import literatureServer from "@src/services/literatureServer";
import literatureSql from "@src/sql/literatureSql";
import literature from "@src/models/literature";
import LiteratureSql from "@src/sql/literatureSql";

const paths = {
  // 字典id
  list: "/literature-list/:id",
  // 文献id
  detail: "/literature-detail/:id",
  // 推荐文献
  recommend: "/literature-recommend",
};

// 文献列表
const list = async (req: Request, res: Response) => {
  const page = req.query.page ? req.query.page : 1;
  const size = req.query.size ? req.query.size : 15;
  const filters = req.query.filters?req.query.filters:{};
  const {id} = req.params;
  const data: any = await literatureServer.frontList(Number(page), Number(size), id,filters);
  return res.status(data.code).json({data});
};

// 文献详情
const detail = async (req: Request, res: Response) => {
  const {id} = req.params;
  const data = await literatureSql.detail(id);
  // 字段+1
  await literature.increment({visit_num: 1}, {where: {id}});
  return res.status(data.code).json({
    data,
  });
};

// 推荐文献
const recommend = async (req: Request, res: Response) => {
  const filters = req.query.filters ? req.query.filters : {};
  const data = await LiteratureSql.recommend(filters);
  return res.status(data.code).json({
    data,
  });
};

export default {
  paths,
  list,
  detail,
  recommend,
};